package app;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;

import com.lidroid.xutils.util.LogUtils;

import mainUI.AppActivity;
import mainUI.AppFragment;
import menhoo.com.i8officelibrary.R;
import model.TabModel;

/**
 * 单独使用Frament时候的载体activity
 * Created by yanglei on 15/9/21.
 */

public class AutoFragment extends AppActivity implements AppFragment.BackHandledInterface {

    TabModel model;

    /**
     * 关闭的时候是否可以返回结果
     */
    public boolean IsReturnResult = false;
    /**
     * 可用来传递对象
     */
    Bundle TagBundle;


    private AppFragment mFragment;

    @Override
    public void setSelectedFragment(AppFragment selectedFragment) {
        this.mFragment = selectedFragment;
    }

    @Override
    public void onBackPressed() {
        if (mFragment == null || !mFragment.onBackPressed()) {
            if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
                super.onBackPressed();
            } else {
                getSupportFragmentManager().popBackStack();
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setRunSignalrService(true);
        super.onCreate(savedInstanceState);
        //
        setContentView(R.layout.auto_fragment);


        try {
            model = (TabModel) getIntent().getExtras().get("Model");
            setTitle(model.name);
        } catch (Exception e) {
            model = null;
        }

        try {
            IsReturnResult = getIntent().getExtras().getBoolean("IsReturnResult");
        } catch (Exception e) {
            LogUtils.e(e.getMessage() + "");
        }

        try {
            TagBundle = getIntent().getBundleExtra("TagBundle");

        } catch (Exception e) {
            LogUtils.e(e.getMessage() + "");
        }

        setTitleDisplay(false);

        initFragment(model);
    }

    public void initFragment(TabModel tab) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        try {
            Fragment fragmentObj = (Fragment) tab.getFragment().newInstance();
            Bundle mBundle = new Bundle();
            mBundle.putString("Title", tab.name);
            mBundle.putString("Url", tab.htmlUrl);
            mBundle.putBundle("TagBundle", TagBundle);

            if (tab.code.equalsIgnoreCase("Apps") || tab.name.equalsIgnoreCase("应用")) {
                mBundle.putBoolean("IsShowTitle", true);
                mBundle.putBoolean("IsShowBtnBack", true);
                mBundle.putBoolean("IsReturnResult", IsReturnResult);
            } else if (tab.code.equalsIgnoreCase("People") || tab.name.equalsIgnoreCase("通讯录")) {
                mBundle.putBoolean("IsShowTitle", true);
                mBundle.putBoolean("IsShowBtnBack", true);
                mBundle.putBoolean("IsReturnResult", IsReturnResult);
            } else if (tab.type.equalsIgnoreCase("html")) {
                mBundle.putBoolean("IsShowTitle", false);
                mBundle.putBoolean("IsShowBtnBack", false);
            } else if (tab.code.equalsIgnoreCase("IM")) {
                setIsImHint(false);
                mBundle.putBoolean("IsShowTitle", true);
                mBundle.putBoolean("IsShowBtnBack", true);
            } else {
                mBundle.putBoolean("IsShowTitle", true);
                mBundle.putBoolean("IsShowBtnBack", true);
            }
            fragmentObj.setArguments(mBundle);
            transaction.replace(R.id.fragment_main, fragmentObj);
            //transaction.add(R.id.fragment_main,fragmentObj);
            transaction.commit();
        } catch (Exception e) {

        }


    }

}
